<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>邮件管理</title>
    {% load staticfiles %}
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="//at.alicdn.com/t/font_693759_wytlyqhtdtj1nhfr.css">

    <link rel="stylesheet" href="{% static 'lib/layui/css/layui.css' %}">
    <link rel="stylesheet" href="{% static 'css/scroll-bar.css' %}">
    <link rel="stylesheet" href="{% static 'css/sub-page.css' %}">


</head>
<body>
<div class="ok-body">
    <!--面包屑导航区域-->
    <div class="ok-body-breadcrumb">
            <span class="layui-breadcrumb">
                <a><cite>首页</cite></a>
                <a><cite>设置</cite></a>
                <a><cite>邮箱设置</cite></a>
            </span>
        <a class="layui-btn layui-btn-small" href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh"></i>
        </a>
    </div>

<form class="layui-form layui-form-pane ok-form" lay-filter="edit-emali-setting">

    <div class="layui-form-item">
        <label class="layui-form-label">Email主题前缀</label>
        <div class="layui-input-block">
            <input type="text" name="EMAIL_SUBJECT_PREFIX" placeholder="EMAIL_SUBJECT_PREFIX" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>


    <div class="layui-form-item">
            <label class="layui-form-label">SMTP主机</label>
            <div class="layui-input-block">
                <input type="text" name="EMAIL_HOST" placeholder="EMAIL_HOST" autocomplete="off" class="layui-input"
                       lay-verify="required">
            </div>
        </div>

            <div class="layui-form-item">
            <label class="layui-form-label">SMTP端口</label>
            <div class="layui-input-block">
                <input type="text" name="EMAIL_PORT" placeholder="EMAIL_PORT" autocomplete="off" class="layui-input"
                       lay-verify="number">
            </div>
        </div>

            <div class="layui-form-item">
            <label class="layui-form-label">SMTP账号</label>
            <div class="layui-input-block">
                <input type="text" name="EMAIL_HOST_USER" placeholder="EMAIL_HOST_USER" autocomplete="off" class="layui-input"
                       lay-verify="required">
            </div>
        </div>

            <div class="layui-form-item">
            <label class="layui-form-label">SMTP密码</label>
            <div class="layui-input-block">
                <input type="password" name="EMAIL_HOST_PASSWORD" placeholder="EMAIL_HOST_PASSWORD" autocomplete="off" class="layui-input"
                       lay-verify="required">
            </div>
        </div>

            <div class="layui-form-item">
            <label class="layui-form-label">使用SSL</label>
            <div class="layui-input-block">
                <input type="checkbox" name="EMAIL_USE_SSL" lay-skin="switch" lay-text="启用|禁用"  value="0">
                                <b >如果SMTP端口是465，通常需要启用SSL</b>
            </div>
        </div>

            <div class="layui-form-item">
            <label class="layui-form-label">使用TLS</label>
            <div class="layui-input-block">
                <input type="checkbox" name="EMAIL_USE_TLS" lay-skin="switch" lay-text="启用|禁用"  value="0">
                <b >如果SMTP端口是587，通常需要启用TLS</b>
            </div>

        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="test">测试连接</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                <button class="layui-btn" lay-submit lay-filter="submit">提交</button>
            </div>
        </div>
    </form>



</div>
<!--js逻辑-->
<script src="{% static 'lib/layui/layui.js' %}"></script>
<script>
    layui.use(['element', 'form', 'jquery', 'laydate'], function () {
        var element = layui.element
            , table = layui.table
            , form = layui.form
            , $ = layui.jquery
            , laydate = layui.laydate;

        laydate.render({
            elem: '#birthday',
            type: "datetime"
        });


        form.on('submit(submit)', function (data) {
            console.log(data.field);
            $.ajax({
                type: 'post',
                url: "{% url 'api_email_setting_edit' %}", // ajax请求路径
                data: {
                    data: JSON.stringify(data.field),
                    csrfmiddlewaretoken: "{{ csrf_token }}",
                },
                dataType: 'json',
                success: function (data) {
                    if (data["status"] == 0) {
                        layer.msg("修改成功！", {icon: 6, time: 1000, anim: 4}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        });
                    } else if (data["status"] == 1) {
                        layer.msg('修改失败,键值对冲突,请检查');
                    }
                }
            });
            return false;
        });


        form.on('submit(test)', function (data) {
            console.log(data.field);
            $.ajax({
                type: 'post',
                url: "{% url 'api_email_setting_test' %}", // ajax请求路径
                data: {
                    data: JSON.stringify(data.field),
                    csrfmiddlewaretoken: "{{ csrf_token }}",
                },
                dataType: 'json',
                success: function (data) {
                    if (data["status"] == 0) {
                        layer.msg("发送成功！", {icon: 6, time: 1000, anim: 4}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        });
                    } else if (data["status"] == 1) {
                        layer.msg('发送失败,请检查'+data["message"]);
                    }
                }
            });
            return false;
        });


        $.ajax({
            type: 'post',
            url: "{% url 'api_email_setting_get' %}", // ajax请求路径
            data: {
                csrfmiddlewaretoken: "{{ csrf_token }}",
            },
            dataType: 'json',
            success: function (data) {
                console.log(data);
                form.val('edit-emali-setting', data);
            }
        });


    })
</script>
</body>
</html>